package com.example.demo.service;

import com.example.demo.domain.BlogPost;
import com.example.demo.mapper.BlogPostMapper;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class BlogService {
    private final BlogPostMapper blogPostMapper;

    public BlogService(BlogPostMapper blogPostMapper) {
        this.blogPostMapper = blogPostMapper;
    }
//    private List<BlogPost> posts = Arrays.asList(
//            new BlogPost("First Post", "This is my first blog post"),
//            new BlogPost("Second Post", "This is my second blog post"),
//            new BlogPost("Third Post", "This is my third blog post")
//    );
    public List<BlogPost> getAllBlogPosts(){
        return blogPostMapper.findAll();
    }

    public BlogPost getBlogPostById(Long id){
//        return posts.stream()
//                .filter(p -> p.getId().equals(id))
//                .findFirst()
//                .orElseThrow(() -> new IllegalArgumentException("Invalid post id: " + id));
        return blogPostMapper.findById(id);
    }

    public void saveBlogPost(BlogPost post){
        if(post.getId() == null){
            blogPostMapper.insert(post);
        }else{
            blogPostMapper.update(post);
        }
    }
}
